package pers.bc.utils.pub;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * * Created with Description: 提供一些对象有效性校验的方法
 *
 * @fileRelativePath: JudgeAssertUtil.java
 * @Date: Created on 2018/11/4 14:24 <br>
 * @author: LiBencheng <br>
 * @version: 1.0 <br>
 */
public class JudgeAssertUtil
{
    private  static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(JudgeAssertUtil.class);
    public static String logSplitStr = "SB`";
    private static String fileDir = "thwlogs";
    // private static Logger logger = (Logger) LoggerFactory.getLogger(JudgeAssertUtil.class);
    
    /**
     * *********************************************************** <br>
     * *说明： 斷言：最高法法官判斷，自定义日志文件夹<br>
     * @see <br>
     * @param judge 斷言
     * @param message
     * @void <br>
     * @methods pers.bc.utils.pub.JudgeAssertUtil#checkAssert <br>
     * @author LiBencheng <br>
     * @date Created on 2022年3月31日 <br>
     * @time 00:25:31 <br>
     * @version 1.0 <br>
     *************************************************************          <br>
     */
    public static void checkAssertDir(boolean judge, String folder, String message)
    {
        checkAssertFormatDir(judge, folder, message);
    }
    
    public static void checkAssertDir(boolean judge, String folder, String message, Object... args)
    {
        checkAssertFormatDir(judge, folder, message, args);
    }
    
    public static void checkAssertFormatDir(boolean judge, String folder, String message, Object... args)
    {
        if (judge)
        {
            throwExceptionDir(new UnsupportedOperationException(String.format(logSplitStr + message, args)), folder);
        }
    }
    
    public static void checkNullDir(Object object, String folder, String message)
    {
        checkNullFormatDir(object, folder, message, "");
    }
    
    public static void checkNullFormatDir(Object object, String folder, String message, Object... args)
    {
        if (object == null)
        {
            throwExceptionDir(new NullPointerException(String.format(logSplitStr + "NPE/EMPTY：" + message, args)), folder);
        }
    }
    
    public static void checkEmptyDir(Object object, String folder, String message)
    {
        checkEmptyFormatDir(object, folder, message, "");
    }
    
    public static void checkEmptyFormatDir(Object object, String folder, String message, Object... args)
    {
        if (PubEnvUtil.isEmptyObj(object))
        {
            throwExceptionDir(new NullPointerException(String.format(logSplitStr + "NPE/EMPTY：" + message, args)), folder);
        }
    }
    
    /*************************************************************
     * <br>
     *
     * 说明: 斷言：最高法法官判斷<br>
     *
     * @Description <br>
     * @Date Created on 2022/4/1 14:08 <br>
     * @param judge
     * @param message <br>
     * @return void <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     *************************************************************          <br>
     */
    public static void checkAssert(boolean judge, String message)
    {
        checkAssertFormat(judge, message);
    }
    
    public static void checkAssert(boolean judge, String message, Object... args)
    {
        checkAssertFormat(judge, message, args);
    }
    
    public static void checkAssertFormat(boolean judge, String message, Object... args)
    {
        if (judge)
        {
            throwException(new UnsupportedOperationException(String.format(logSplitStr + message, args)));
        }
    }
    
    public static void checkAssert(boolean judge, Throwable e)
    {
        if (judge) throwException(e);
    }
    
    public static void checkAssertDir(boolean judge, String folder, Throwable e)
    {
        if (judge) throwExceptionDir(e, folder);
    }
    
    public static void checkNull(Object object, String message)
    {
        checkNullFormat(object, fileDir, message, "");
    }
    
    public static void checkNullFormat(Object object, String message, Object... args)
    {
        if (object == null)
        {
            throwException(new NullPointerException(String.format(logSplitStr + "NPE/EMPTY：" + message, args)));
        }
    }
    
    public static void checkEmpty(Object object, String message)
    {
        checkEmptyFormat(object, fileDir, message, "");
    }
    
    public static void checkEmptyFormat(Object object, String message, Object... args)
    {
        if (PubEnvUtil.isEmptyObj(object))
        {
            throwException(new NullPointerException(String.format(logSplitStr + "NPE/EMPTY：" + message, args)));
        }
    }
    
    public static void throwException(String throwMsg, Object... args)
    {
        throwException(new RuntimeException(String.format(logSplitStr + throwMsg, args)));
    }
    
    public static void throwException(Throwable e)
    {
        throwException(e, fileDir, "");
    }
    
    public static void throwException(String message, Throwable e)
    {
        throwException(e, fileDir, message);
    }
    
    public static void throwExceptionDir(String throwMsg, String folder, Object... args)
    {
        throwExceptionDir(new RuntimeException(String.format(logSplitStr + throwMsg, args)), folder);
    }
    
    public static void throwExceptionDir(Throwable e, String folder)
    {
        throwException(e, folder + File.separator + fileDir, "");
    }
    
    public static void throwException(Throwable e, String file, String message)
    {
        if (PubEnvUtil.isEmpty(file)) file = fileDir;
        // logger.info("===================== throwException ======================");
        // String msg = StringUtil.valueOfDefault(e.getMessage(), " 🖥🛠️📝📌🌈 >_< 后台操作异常，联系管理员進行日志排查！！！");
        String msg = StringUtil.valueOfDefault(e.getMessage(), "后台操作异常，联系管理员進行日志排查！！！");
        String[] bcs = msg.split(logSplitStr);
        msg = (PubEnvUtil.getSize(bcs) > 1 ? bcs[1] : msg);
        msg = (PubEnvUtil.containStr(message, new String[]{msg})) ? message : msg;
        if (msg.contains("后台操作异常"))//
            msg = processExcepMessage(e, msg);
        
        String msgTemp = message.equals(e.getMessage()) ? e.getMessage() : e.getMessage() + "@@@" + message;
        msgTemp = msgTemp.equals(msg) ? msgTemp : msgTemp + LoggerUtil.SLASH + msg;
        LoggerUtil.getInstance(file).error(LoggerUtil.getInvokMethod(2), "   Caused by ---> 👊👊👊👉" + msgTemp);
        LoggerUtil.getInstance(file).savaLogger(LoggerUtil.getInvokMethod(2), LoggerUtil.CRLF + "   Caused by ---> 👊👊👊👉" + msgTemp, e);
        
        RuntimeException retExceptopn = new RuntimeException("👉" + msg);
        // retExceptopn.setStackTrace(e.getStackTrace());
//        retExceptopn.addSuppressed(e);
         retExceptopn.initCause(e);
        
        System.err.print(StringUtil.toString(retExceptopn));
        // logger.error(StringUtil.toString(retExceptopn));
        
        throw retExceptopn;
    }
    
    private static String processExcepMessage(Throwable e, String message)
    {
        Throwable cause = e.getCause();
        if (e instanceof UndeclaredThrowableException)
            processExcepMessage(((UndeclaredThrowableException) e).getUndeclaredThrowable(), e.getClass().getName() + "👈" + message);
        else if (e instanceof InvocationTargetException)
            processExcepMessage(((InvocationTargetException) e).getTargetException(), e.getClass().getName() + "👈" + message);
        // message = (e.getClass().getName() + "----" + ((InvocationTargetException)
        // e).getTargetException().toString() + "👈" + message);
        // else if (e instanceof NoClassDefFoundError)
        // message = (e.getClass().getName() + e.getMessage() + "👈" + message);
        // else if (e instanceof ClassCastException)
        // message = (e.getClass().getName() + e.getMessage() + "👈" + message);
        // else if (e instanceof ClassNotFoundException)
        // message = (e.getClass().getName() + e.getMessage() + "👈" + message);
        // else if (PubEnvUtil.isNotNullObj(cause)) processExcepMessage(cause, e.getClass().getName() + "👈" +
        // message);
        else
            message = (e.getClass().getName() + e.getMessage() + "👈" + message);
        
        return message;
    }
    
    public static String processMessage(Object message)
    {
        String[] bcs = StringUtil.valueOfNull(message).split(logSplitStr);
        return PubEnvUtil.getSize(bcs) > 1 ? bcs[1] : StringUtil.toString(message);
    }
    
    public static String processMsg(Object message)
    {
        String[] bcs = StringUtil.valueOfNull(message).split(PubEnvUtil.COLON);
        return PubEnvUtil.getSize(bcs) > 1 ? bcs[PubEnvUtil.getSize(bcs) - 1] : StringUtil.toString(message);
    }
    
    public static String assertNoN(Object obj, String message)
    {
        return PubEnvUtil.isNotEmptyObj(obj) ? "" : message;
    }
    
}
